<?

//Data loading
if (GetParam("periodMonth","")=="AUDIT")
    $title='RATIO ANALYSIS PERFORMANCE REPORT (AUDIT)';
else
    $title='RATIO ANALYSIS PERFORMANCE REPORT';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();

//$Result = GridList ();


$pdf->GridTable();
$pdf->Output();

function Neraca ($PYear,$PMonth) {
    global $DBConnection;

    //$TEMP = "TEMPORARY";

    $Year   = $PYear;
    $LastYear = $Year-1;
    $Month  = $PMonth;
    if ($Month=="AUDIT") $Month=13;
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) $LastMonth = "0".($Month-1);

    if ($Month<2) $LastMonth = "awal as lastmonth";
    else {$LastMonth = "aku".$LastMonth." as lastmonth";}

    $Mutasi = "(mutd".$Month."-"."mutk".$Month.") as mutasi";
    $CurrentMonth = "aku".$Month." as cmonth";

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp_last`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp_last` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp_last
        SELECT skada, $CurrentMonth, ktrasl
        FROM tblcoa WHERE tipe=1 AND ledger='$LastYear'
    ";

    if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp
        SELECT tblcoa.skada, tblcoa.pathprn, tblcoa.namacc, tblcoa.parent, tblcoa.printing, tblcoa.jumlah, tblcoa.grup, $CurrentMonth, tblcoatmp_last.akumulasi, tblcoa.ktrasl
        FROM tblcoa LEFT JOIN tblcoatmp_last ON tblcoatmp_last.skada=tblcoa.skada AND tblcoatmp_last.ktrasl=tblcoa.ktrasl
        WHERE tipe=1 AND ledger='$Year'
    ";

    if (GetParam("office",""))      $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoa_totaltmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoa_totaltmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL  = "
        INSERT INTO tblcoa_totaltmp
        SELECT tblcoatmp.skada, tblcoatmp.pathprn, tblcoatmp.namacc, tblcoatmp.parent, tblcoatmp.printing,
                tblcoatmp.jumlah, tblcoatmp.grup, sum(aku$Month), lastakumulasi, tblcoa.ktrasl
        FROM tblcoatmp, tblcoa
        WHERE tblcoa.pathprn like concat(tblcoatmp.pathprn,'%') AND tblcoatmp.ktrasl=tblcoa.ktrasl
    ";

    if (GetParam("office","")) $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."' ";

    $SQL .= "GROUP BY tblcoatmp.skada";
        //print $SQL."<p>\n";die();
    $DBConnection->dbc->query($SQL);
}

function SummarizeParent () {
    global $DBConnection,$SUBTOTAL_LAST,$SUBTOTAL_CURR,$SUBTOTAL_MUT,$Inverse;

    $SQL = "
        SELECT skada, namacc, pathprn, parent, printing, jumlah, total, group_name, coatype, aku_last, aku_curr, mutasi, lastyear
        FROM tblcoa_totaltmp, tblcoagroup
        WHERE parent='' AND (coatype='REVENUES' OR coatype='EXPENSES' OR coatype='OTHER EXPENSES') AND tblcoa_totaltmp.grup=tblcoagroup.group_id
    ";

    //print $SQL."<p>\n";
    $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
    if ($MyResult) {
        foreach ($MyResult as $RowResult) {
                $SUBTOTAL_LAST = 0;
                $SUBTOTAL_CURR = 0;
                $SUBTOTAL_MUT  = 0;

                $SUBTOTAL_LAST  = $RowResult["aku_last"];
                $SUBTOTAL_CURR  = $RowResult["aku_curr"];
                $SUBTOTAL_MUT   = $RowResult["mutasi"];
                $SUBTOTAL_LASTY = $RowResult["lastyear"];
        }
    }

}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
//  $strdate = date()
  return $arraymonth[$month].", $year";
}

function Get_NetIncome() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60' ";
  $acc_skada60 = $DBConnection->dbc->get_var($SQL);
//  $acc_skada60 = $acc_skada60<0?$acc_skada60*-1:$acc_skada60;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' ";
  $acc_skada71 = $DBConnection->dbc->get_var($SQL);
//  $acc_skada71 = $acc_skada71*-1;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='72' ";
  $acc_skada72 = $DBConnection->dbc->get_var($SQL);
//  $acc_skada72 = $acc_skada72<0?$acc_skada72*-1:$acc_skada72;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='73' ";
  $acc_skada73 = $DBConnection->dbc->get_var($SQL);
//  $acc_skada73 = $acc_skada73<0?$acc_skada73*-1:$acc_skada73;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80' ";
  $acc_skada80 = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90' ";
  $acc_skada90 = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='91' ";
  $acc_skada91 = $DBConnection->dbc->get_var($SQL);
//  return $acc_skada60+$acc_skada71+$acc_skada72+$acc_skada73+$acc_skada80+$acc_skada90+$acc_skada91;
  return ($acc_skada6-$acc_skada71-$acc_skada72-$acc_skada73-$acc_skada80+$acc_skada90+$acc_skada91)*-1;
}

function Get_CashRatio() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0001' OR skada='0002' OR skada='0003'";
  $cash_acc = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $liability_acc = $DBConnection->dbc->get_var($SQL);
  $liability_acc = $liability_acc<0?$liability_acc*-1:$liability_acc;
  return $cash_acc/$liability_acc;
}

function Get_CurrentRatio() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='00'";
  $curasset_acc = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $curliability_acc = $DBConnection->dbc->get_var($SQL);
  $curliability_acc = $curliability_acc<0?$curliability_acc*-1:$curliability_acc;
  return $curasset_acc/$curliability_acc;
}

function Get_AcidRatio() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0001' OR skada='0002' OR skada='0003' OR skada='0005' OR skada='0006'";
  $curasset_acc = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $curliability_acc = $DBConnection->dbc->get_var($SQL);
  $curliability_acc = $curliability_acc<0?$curliability_acc*-1:$curliability_acc;
  return $curasset_acc/$curliability_acc;
}

function Get_DebttoEquityRatio() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $curliability_acc = $DBConnection->dbc->get_var($SQL);
  $curliability_acc = $curliability_acc<0?$curliability_acc*-1:$curliability_acc;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='50'";
  $equity_acc = $DBConnection->dbc->get_var($SQL);
  $equity_acc = $equity_acc<0?$equity_acc*-1:$equity_acc;
  return $curliability_acc/$equity_acc;
}

function Get_LongDebttoEquityRatio() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='40'";
  $curliability_acc = $DBConnection->dbc->get_var($SQL);
  $curliability_acc = $curliability_acc<0?$curliability_acc*-1:$curliability_acc;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='50'";
  $equity_acc = $DBConnection->dbc->get_var($SQL);
  $equity_acc = $equity_acc<0?$equity_acc*-1:$equity_acc;
  return $curliability_acc/$equity_acc;
}

function Get_TimeInterestEarned() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $pendapatan = $DBConnection->dbc->get_var($SQL);
  $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
  $biaya = $DBConnection->dbc->get_var($SQL);

  $gross_profit=$pendapatan-$biaya;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
  $biaya_umum = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90'";
  $biaya_other = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='91'";
  $biaya_pajak = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='9002'";
  $biaya_bunga = $DBConnection->dbc->get_var($SQL);


  $TimeInterestEarned=($gross_profit-$biaya_umum-$biaya_other+$biaya_bunga)/$biaya_bunga*100;

  return $TimeInterestEarned;
}

function Get_GrossProfitMargin() {
  global $DBConnection;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $pendapatan = $DBConnection->dbc->get_var($SQL);
  $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
  $biaya = $DBConnection->dbc->get_var($SQL);

  $gross_profit=$pendapatan-$biaya;
  $gross_profit_margin=$gross_profit/$pendapatan*100;
  return $gross_profit_margin;
}


function Get_OperatingProfitMargin() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $pendapatan = $DBConnection->dbc->get_var($SQL);
  $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
  $biaya = $DBConnection->dbc->get_var($SQL);

  $gross_profit=$pendapatan-$biaya;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
  $biaya_umum = $DBConnection->dbc->get_var($SQL);

  $operating_profit_margin=($gross_profit-$biaya_umum)/$pendapatan*100;

  return $operating_profit_margin;
}


function Get_PretaxProfitMargin() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $pendapatan = $DBConnection->dbc->get_var($SQL);
  $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
  $biaya = $DBConnection->dbc->get_var($SQL);

  $gross_profit=$pendapatan-$biaya;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
  $biaya_umum = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90'";
  $biaya_other = $DBConnection->dbc->get_var($SQL);

  $operating_pretax_margin=($gross_profit-$biaya_umum-$biaya_other)/$pendapatan*100;

  return $operating_pretax_margin;
}

function Get_NetIncomeMargin() {
  global $DBConnection;
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $pendapatan = $DBConnection->dbc->get_var($SQL);
  $pendapatan = $pendapatan<0?$pendapatan*-1:$pendapatan;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='71' OR  skada='72' OR skada='73'";
  $biaya = $DBConnection->dbc->get_var($SQL);

  $gross_profit=$pendapatan-$biaya;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='80'";
  $biaya_umum = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='90'";
  $biaya_other = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='91'";
  $biaya_pajak = $DBConnection->dbc->get_var($SQL);


  $NetIncomemargin=($gross_profit-$biaya_umum-$biaya_other-$biaya_pajak)/$pendapatan*100;

  return $NetIncomemargin;
}


function Get_CollectionPeriod() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
  $current_AR = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;
  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
  $last_AR = $DBConnection->dbc->get_var($SQL);

  if (!$last_AR) $last_AR=0;

  $collection_period =  (($current_AR+$last_AR)/2)/($current_Revenue/360);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($collection_period,2);
}

function Get_CashTurnOver() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0001' OR skada='0002' OR skada='0003' OR skada='0004'OR skada='0005'";
  $current_cash_equivalent = $DBConnection->dbc->get_var($SQL);
  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0001' OR skada='0002' OR skada='0003' OR skada='0004'OR skada='0005'";
  $last_cash_equivalent = $DBConnection->dbc->get_var($SQL);
  if (!$last_cash_equivalent) $last_cash_equivalent=0;

  $cash_turnover = $current_Revenue/(($last_cash_equivalent+$current_cash_equivalent)/2);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($cash_turnover,2);
}

function Get_ARTurnOver() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
  $current_AR = $DBConnection->dbc->get_var($SQL);
  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));
  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='0006'";
  $last_AR = $DBConnection->dbc->get_var($SQL);

  if (!$last_AR) $last_AR=0;

  $ar_turnover = $current_Revenue/(($last_AR+$current_AR)/2);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($ar_turnover,2);
}

function Get_WCTurnOver() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='00'";
  $current_aktiva_lancar = $DBConnection->dbc->get_var($SQL);

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $current_pasiva_lancar = $DBConnection->dbc->get_var($SQL);
  $current_pasiva_lancar = $current_pasiva_lancar<0?$current_pasiva_lancar*-1:$current_pasiva_lancar;

  $current_WC = $current_aktiva_lancar - $current_pasiva_lancar;

  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='00'";
  $last_aktiva_lancar = $DBConnection->dbc->get_var($SQL);
  if (!$last_aktiva_lancar) $last_aktiva_lancar=0;


  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='30'";
  $last_pasiva_lancar = $DBConnection->dbc->get_var($SQL);
  $last_pasiva_lancar = $last_pasiva_lancar<0?$last_pasiva_lancar*-1:$last_pasiva_lancar;
  if (!$last_pasiva_lancar) $last_pasiva_lancar=0;

  $last_WC = $last_aktiva_lancar - $last_pasiva_lancar;
  if (!$last_WC) $last_WC=0;

  $wc_turn_over = $current_Revenue/(($current_WC+$last_WC)/2);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($wc_turn_over,2);
}

function Get_FATurnOver() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='10'";
  $current_fa = $DBConnection->dbc->get_var($SQL);

  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='10'";
  $last_fa = $DBConnection->dbc->get_var($SQL);

  if (!$last_fa) $last_fa=0;

  $fa_turn_over = $current_Revenue/(($current_fa+$last_fa)/2);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($fa_turn_over,2);
}

function Get_TATurnOver() {
  global $DBConnection;
  if ((GetParam("periodMonth","")<12) && (GetParam("periodMonth","")!="AUDIT")) return 'N/A';

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='60'";
  $current_Revenue = $DBConnection->dbc->get_var($SQL);
  $current_Revenue = $current_Revenue<0?$current_Revenue*-1:$current_Revenue;

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='00' OR skada='10' OR skada='20'";
  $current_ta = $DBConnection->dbc->get_var($SQL);

  if (GetParam("periodMonth","")=="AUDIT")
  Neraca (GetParam("periodYear","")-1,13);
  else
  Neraca (GetParam("periodYear","")-1,GetParam("periodMonth",""));

  $SQL = "SELECT SUM(akumulasi) FROM tblcoa_totaltmp WHERE skada='00' OR skada='10' OR skada='20'";
  $last_ta = $DBConnection->dbc->get_var($SQL);

  if (!$last_ta) $last_ta=0;

  $ta_turn_over = $current_Revenue/(($current_ta+$last_ta)/2);
  Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));
  return number_format($ta_turn_over,2);
}

class PDFReport extends PDF {
    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }

        $this->SetFont('Arial','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);

        if (GetParam("m","")=="AUDIT")
        $this->Cell(200,5,"PERIOD: ".strtoupper(date("d F, Y", mktime(0, 0, 0, 13, 0, GetParam("fromYear","")))),0,1,'C',0);
        else
        $this->Cell(200,5,"PERIOD: ".strtoupper(date("d F, Y", mktime(0, 0, 0, GetParam("periodMonth","")+1, 0, GetParam("periodYear","")))),0,1,'C',0);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(10,30,210,30);
        $this->Ln();
        $this->Ln();
        //Header

        //Column widths
        $w=array(50,30);
        $this->SetFont('Arial','',8);
        $this->Cell($w[0],5,"DESCRIPTION","1",0,'C',0);
        $this->Cell($w[1],5,"RATIO","1",0,'C',0);
        $this->Ln();
    }

    function GridTable() {
        //Column widths
        $w=array(50,30);

        $this->Cell($w[0],5,"",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();
        Neraca (GetParam("periodYear",""),GetParam("periodMonth",""));

        //LIQUIDITY RATIO
        $this->Cell($w[0],5,"LIQUIDITY RATIO",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Cash Ratio",'LR');
        $this->Cell($w[1],5,number_format(Get_CashRatio(),2),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Current Ratio",'LR');
        $this->Cell($w[1],5,number_format(Get_CurrentRatio(),2),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Acid Test Ratio",'LR');
        $this->Cell($w[1],5,number_format(Get_AcidRatio(),2),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Collection Period",'LR');
        $this->Cell($w[1],5,Get_CollectionPeriod(),'LR',0,'R');
        $this->Ln();

        //CAPITAL STRUCTURE & SOLVENCY
        $this->Cell($w[0],5,"CAPITAL STRUCTURE & SOLVENCY",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Debt to Equity Ratio",'LR');
        $this->Cell($w[1],5,number_format(Get_DebttoEquityRatio(),2),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Long Term Debt to Equity Ratio",'LR');
        $this->Cell($w[1],5,number_format(Get_LongDebttoEquityRatio(),2),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Time Interest Earned",'LR');
        $this->Cell($w[1],5,number_format(Get_TimeInterestEarned(),2),'LR',0,'R');
        $this->Ln();

        //PROFITABILITY RATIO
        $this->Cell($w[0],5,"PROFITABILITY RATIO",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Gross Profit Margin",'LR');
        $this->Cell($w[1],5,number_format(Get_GrossProfitMargin(),2)."%",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Operating Profit Margin",'LR');
        $this->Cell($w[1],5,number_format(Get_OperatingProfitMargin(),2)."%",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Pretax Profit Margin",'LR');
        $this->Cell($w[1],5,number_format(Get_PretaxProfitMargin(),2)."%",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Net Income Margin",'LR');
        $this->Cell($w[1],5,number_format(Get_NetIncomeMargin(),2)."%",'LR',0,'R');
        $this->Ln();

        //ASSET UTILIZATION
        $this->Cell($w[0],5,"ASSETS UTILIZATION",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Cash Turnover",'LR');
        $this->Cell($w[1],5,Get_CashTurnOver(),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   A/R Turnover",'LR');
        $this->Cell($w[1],5,Get_ARTurnOver(),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Working Capital Turn Over",'LR');
        $this->Cell($w[1],5,Get_WCTurnOver(),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Fixed Assets Turnover",'LR');
        $this->Cell($w[1],5,Get_FATurnOver(),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"   Total Assets Turnover",'LR');
        $this->Cell($w[1],5,Get_TATurnOver(),'LR',0,'R');
        $this->Ln();
        $this->Cell($w[0],5,"",'LR');
        $this->Cell($w[1],5,"",'LR',0,'R');
        $this->Ln();

        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(80,0,'','T');
    }
}
?>
